package com.ssyc.gsk_teacher.activity;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.BqaManager;
import com.ssyc.common.manager.PopUpManager;
import com.ssyc.common.manager.SrfManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.adapter.TeacherShowAdapter;
import com.ssyc.gsk_teacher.bean.TeacherShowInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 教师端--教师风采界面
 * Created by Kevin on 2018/6/27
 */

@Route(path = "/teacher/TeacherShowActivity")
public class TeacherShowActivity extends BaseActivity implements View.OnClickListener, SrfManager.OnPullRefreshListener {

    //返回键
    private ImageView ivBack;

    private ImageView lvTeach;

    private ImageView lvHard;

    //Tab页面选中的资源图片集合
    private int[] tabSelectIds = {R.drawable.teacher_show_shaike_select, R.drawable.teacher_show_pingke_select};

    //Tab页面未选中的资源图片集合
    private int[] tabUnselectIds = {R.drawable.teacher_shaike_normal, R.drawable.teacher_pingke_normal};

    //gradeID
    private String gradeId;

    private String lessonId;

    private String classId;


    private RecyclerView rv;
    private SmartRefreshLayout srf;
    private RelativeLayout rlLoadingView;

    //空视图
    private View emptyView;

    //数据集合
    private List<TeacherShowInfo.ListBean> oldDatas;

    //适配器
    private TeacherShowAdapter adapter;

    //上传
    private ImageView iv_upload;

    //排序
    private TextView tv_sort;

    //评课晒课类型
    private String type2 = "0";


    //排序方式
    public int SORT = 0;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.teacher_activity_show;
    }

    @Override
    protected void init() {

        showContent();

        //初始化视图
        initView();

        //接收数据
        InitIntent();

        //初始化下来刷新控件
        initSrf();

        //请求数据
        http(BqaManager.NORML);
    }

    /**
     * 接收数据
     */
    private void InitIntent() {
        gradeId = getIntent().getStringExtra("gradeId");
        lessonId = getIntent().getStringExtra("lessonId");
        classId = getIntent().getStringExtra("classId");
    }

    /**
     * 初始化视图的方法
     */
    public void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        lvTeach = findViewById(R.id.lv_teach);
        lvTeach.setOnClickListener(this);
        lvHard = findViewById(R.id.lv_hard);
        lvHard.setOnClickListener(this);

        rv = findViewById(R.id.rv);
        srf = findViewById(R.id.srf);
        rlLoadingView = findViewById(R.id.rl_loading_view);
        rlLoadingView.setVisibility(View.VISIBLE);
        oldDatas = new ArrayList<>();


        iv_upload = findViewById(R.id.iv_upload);
        iv_upload.setOnClickListener(this);

        tv_sort = findViewById(R.id.tv_sort);
        tv_sort.setOnClickListener(this);

        //非教师端隐藏上传按钮
        if(!"2".equals(AccountUtils.getRole(this))){
            iv_upload.setVisibility(View.GONE);
        }
    }

    /**
     * 初始化下拉刷新控件
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, this);
    }

    /**
     * 模拟数据
     */
    private void http(int method) {

        Map<String, String> map = new HashMap<>();
        map.put("type", "17");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("type2", type2);
        HttpUtils.post(HttpAdress.TEACHERSHOWMINE, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (null != rlLoadingView && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {

                if (null != rlLoadingView && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }

                if (!TextUtils.isEmpty(response)) {
                    TeacherShowInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, TeacherShowInfo.class);
                    } catch (Exception e) {
                        Log.i("test", "数据解析异常");
                    }
                    Log.i("test", "个人视频数据是：" + response);
                    if (null != info) {
                        if ("200".equals(info.getState())) {

                            List<TeacherShowInfo.ListBean> newDatas = info.getList();



                            oldDatas.clear();

                            if (SORT == 0) {
                                //默认排序
                                oldDatas.addAll(newDatas);
                                //初始化列表

                            } else if (SORT == 1) {
                                //人气最高排序
                                scoreSort(newDatas);
                            } else if (SORT == 2) {
                                //播放最多排序
                                playSort(newDatas);
                            }
                            if (method == BqaManager.NORML) {
                                initRv();
                            } else {
                                BqaManager.updateRvBySmf(method, srf, rv, oldDatas, newDatas, adapter);
                            }


                        } else {
                            Log.i("test", "错误,状态码是:" + info.getState());
                        }
                    } else {
                        Log.i("test", "返回数据为空");
                    }
                } else {
                    Log.i("test", "返回数据为空");
                }
            }
        });

    }

    /**
     * 人气指数排名
     */
    private void scoreSort(List<TeacherShowInfo.ListBean> newDatas) {
        Collections.sort(newDatas, new Comparator<TeacherShowInfo.ListBean>() {

            @Override
            public int compare(TeacherShowInfo.ListBean o1, TeacherShowInfo.ListBean o2) {

                int i = o1.getScore() - o2.getScore();
                if (i == 0) {
                    return o1.getPlayTimes() - o2.getPlayTimes();
                }
                return i;
            }
        });
        oldDatas.addAll(newDatas);
    }

    /**
     * 播放数排名
     */
    private void playSort(List<TeacherShowInfo.ListBean> newDatas) {
        Collections.sort(newDatas, new Comparator<TeacherShowInfo.ListBean>() {

            @Override
            public int compare(TeacherShowInfo.ListBean o1, TeacherShowInfo.ListBean o2) {

                int i = o1.getPlayTimes() - o2.getPlayTimes();
                if (i == 0) {
                    return o1.getScore() - o2.getScore();
                }
                return i;
            }
        });
        oldDatas.addAll(newDatas);
    }


    /**
     * 初始化列表控件
     */
    private void initRv() {
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view, null);
        GridLayoutManager manager = new GridLayoutManager(this, 2);
        adapter = new TeacherShowAdapter(this, R.layout.teacher_rv_show, oldDatas);
        BqaManager.setRv(emptyView, manager, this, adapter, rv, null);
    }


    /**
     * 下拉刷新
     *
     * @parefreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        http(BqaManager.PULLREFRESH);
    }


    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.lv_teach) {
            //晒课
            lvTeach.setImageResource(tabSelectIds[0]);
            lvHard.setImageResource(tabUnselectIds[1]);
            type2 = "0";
            //请求数据
            http(BqaManager.NORML);

        } else if (id == R.id.lv_hard) {
            //评课
            lvTeach.setImageResource(tabUnselectIds[0]);
            lvHard.setImageResource(tabSelectIds[1]);
            type2 = "1";
            //请求数据
            http(BqaManager.NORML);

        } else if (id == R.id.iv_upload) {
            //上传视频
            Intent intent = new Intent(this, TeacherMineShowActivity.class);
            startActivity(intent);
        } else if (id == R.id.tv_sort) {
            //排序方式
            sort();
        }
    }


    /**
     * 排序方式弹窗
     */
    private void sort() {
        PopUpManager.showPopsort(this, R.layout.teacher_pop_video_sort, 1.0f, tv_sort, new PopUpManager.onGetViewListener() {
            @Override
            public void getChildView(View view, int layoutResId) {
                //最多播放
                TextView tv_max = view.findViewById(R.id.tv_max);

                //人气指数
                TextView tv_hot = view.findViewById(R.id.tv_hot);

                //默认播放
                TextView tv_def = view.findViewById(R.id.tv_def);

                tv_max.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tv_sort.setText("播放最多");
                        PopUpManager.dismiss();
                        //刷新排序数据
                        SORT = 2;
                        //请求数据
                        http(BqaManager.NORML);
                    }
                });

                tv_hot.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tv_sort.setText("人气指数");
                        PopUpManager.dismiss();
                        //刷新排序数据
                        SORT = 1;
                        //请求数据
                        http(BqaManager.NORML);

                    }
                });

                tv_def.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tv_sort.setText("默认排序");
                        PopUpManager.dismiss();
                        //刷新排序数据
                        SORT = 0;

                        //请求数据
                        http(BqaManager.NORML);

                    }
                });
            }
        });
    }
}
